package webCam;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;

/**
 *PanelDobleBuffer
 * @author Jesus Ruiz Oliva
 */
public class PanelDobleBuffer extends JPanel {
        private Image offscreen;
        private int x,y;
        private int stateButton;
        private int areaX;
        private int areaY;
        private int areaWidth;
        private int areaHeight;
        
        
        @Override
        public void paint(Graphics g) {

            if (offscreen == null) {
                offscreen = createImage(getSize().width, getSize().height);
            }

            Graphics offScreenGraphics = offscreen.getGraphics();
            super.paint(offScreenGraphics);

            if (stateButton == 1) {
                Graphics2D g2 = (Graphics2D) offScreenGraphics;
                g2.setPaint(Color.red);
                x= areaX;
                y=areaY;
                if (areaWidth<0){
                    x= areaX + areaWidth;
                    areaWidth = Math.abs(areaWidth);                            
                }
                if (areaHeight<0){
                    y= areaY + areaHeight;
                    areaHeight = Math.abs(areaHeight);                            
                }
                    
                Rectangle2D e = new Rectangle2D.Float(x, 
                        y, areaWidth, areaHeight);
                
                g2.draw(e);
            } 
            g.drawImage(offscreen, 0, 0, null);
            offScreenGraphics.dispose();
            
        }
        
        public void setAreaX(int areaX){
            this.areaX = areaX;
        }
        public void setAreaY(int areaY){
            this.areaY = areaY;
        }
        public void setAreaWidth(int areaWidth){
            this.areaWidth = areaWidth;
        }
        public void setAreaHeight(int areaHeight){
            this.areaHeight = areaHeight;
        }
        public void setStateButton(int stateButton){
            this.stateButton = stateButton;
        }
        public int getStateButton(){
             return this.stateButton;
        }
        public int getAreaX(){
            return this.areaX;
        }
        public int getAreaY(){
            return this.areaY;
        }
        public int getAreaWidth(){
            return this.areaWidth;
        }
        public int getAreaHeight(){
            return this.areaHeight;
        }
    }
